package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CollectDao;
import com.gdit.zax.entitys.CourseCollect;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class GetCollectList extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		
		//检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE || uid==Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		CollectDao cdao = dao.getCollectDao();
		ArrayList<CourseCollect> list = cdao.listCourseCollect(uid, 1, 100);
		ArrayList<Map> outlist =  new ArrayList<Map>();
		for (CourseCollect courseCollect : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("imageurl", Constant.WEBRoot + courseCollect.getT_course_pic());
			map.put("id", courseCollect.getT_course_id());
			map.put("time", courseCollect.getT_course_addtime());
			map.put("name", courseCollect.getT_course_name());
			map.put("Introduction", courseCollect.getT_course_introduction());
			map.put("anthorname", courseCollect.getT_user_nick());
			map.put("seesum", courseCollect.getT_course_seesum());
			map.put("collectsum", courseCollect.getT_course_ccollectsum());//课程收藏数量
			map.put("commentsum", courseCollect.getT_course_commentsum());
			map.put("dynamic", courseCollect.getT_course_dynamic());
			map.put("isnew", courseCollect.getIsnew());
			outlist.add(map);
		}
		O.printSuccess(response, outlist);
	}

}
